package com.example.a410.mybt;

import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import static com.example.a410.mybt.MainActivity.mMainActivity;

public class SensorActivity extends AppCompatActivity {

    private Button mDirectionXBtn;
    private Button mDirectionYBtn;
    private Button mDirectionZBtn;
    private Button mSensorSendDataBtn;
    private Switch mSensorControlSwitch;

    private SensorManager mSensorManager;
    private Sensor mSensor;

    private boolean mSensorSendZFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);

        mDirectionXBtn = (Button)findViewById(R.id.sensor_x);
        mDirectionYBtn = (Button)findViewById(R.id.sensor_y);
        mDirectionZBtn = (Button)findViewById(R.id.sensor_z);
        mSensorSendDataBtn = (Button)findViewById(R.id.sensor_send_data);
        mSensorControlSwitch = (Switch)findViewById(R.id.sensor_control_switch);

        mSensorControlSwitch.setChecked(false);
        mSensorSendZFlag = false;
        // 1:获取传感器管理对象
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        mSensorControlSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 如果设备未连接，则返回
                    if (!mMainActivity.getBlueToothIsConnented()) {
                        goBackDialog();
                        mSensorControlSwitch.setChecked(false);
                        return;
                    }
                    AlertDialog.Builder dialog = new AlertDialog.Builder(SensorActivity.this);
                    dialog.setTitle("提示");
                    dialog.setMessage("蓝牙开始发送控制信息");
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (!mMainActivity.getBlueToothIsConnented()) {
                                mSensorControlSwitch.setChecked(false);
                                Toast.makeText(SensorActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    dialog.show();
                } else {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(SensorActivity.this);
                    dialog.setTitle("提示");
                    dialog.setMessage("蓝牙已停止发送控制信息");
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (mMainActivity.getBlueToothIsConnented()) {
                                mMainActivity.sendData("z");
                                mSensorSendDataBtn.setText("z");
                            }
                        }
                    });
                    dialog.show();
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        // 为系统的方向传感器注册监听器
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        mSensorManager.registerListener(listener, mSensor,SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //传感器使用完毕，释放资源
        if(mSensorManager != null){
            mSensorManager.unregisterListener(listener);
        }
    }

    //第三步：对传感器信号进行监听
    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            // 当前传感器类型
            int sensorType = event.sensor.getType();

            float[] values = event.values;
            switch (sensorType) {
                // 方向传感器
                case Sensor.TYPE_ORIENTATION: {
                    mDirectionXBtn.setText("X = " + Float.toString(values[0]));
                    mDirectionYBtn.setText("Y = " + Float.toString(values[1]));
                    mDirectionZBtn.setText("Z = " + Float.toString(values[2]));
                    if (mSensorControlSwitch.isChecked()) {
                        if (mSensorSendZFlag && !(values[2] < 0.0 || values[2] > 40 ||
                                values[1] > 20 || values[1] < -20)) {
                            mMainActivity.sendData("z");
                            mSensorSendDataBtn.setText("z");
                            mSensorSendZFlag = false;
                        }
                        if (values[2] < 0.0) {
                            mMainActivity.sendData("w");
                            mSensorSendDataBtn.setText("w");
                            mSensorSendZFlag = true;
                        }
                        else if (values[2] > 40) {
                            mMainActivity.sendData("s");
                            mSensorSendDataBtn.setText("s");
                            mSensorSendZFlag = true;
                        }
                        else if (values[1] > 20) {
                            mMainActivity.sendData("a");
                            mSensorSendDataBtn.setText("a");
                            mSensorSendZFlag = true;
                        }
                        else if (values[1] < -20) {
                            mMainActivity.sendData("d");
                            mSensorSendDataBtn.setText("d");
                            mSensorSendZFlag = true;
                        }
                        else {
                            mSensorSendDataBtn.setText(" ");
                        }
                    }
                    break;
                }
                default: {
                    break;
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            //传感器的精度发生变化时就会调用 onAccuracyChanged 方法                }
        }
    };

    public void goBackDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(SensorActivity.this);
        dialog.setTitle("设备未连接！");
        dialog.setMessage("设备连接后可以发送数据，请返回重新连接。");
        dialog.setCancelable(false);
        dialog.setPositiveButton("重新连接", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        dialog.setNegativeButton("忽略", null);
        dialog.show();
    }
}
